package miharu;

// import java.io.FileReader;
// import java.io.IOException;
// import java.util.List;

// import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
// import au.com.bytecode.opencsv.bean.CsvToBean;

import java.net.MalformedURLException;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

/**
 *
 * 東京電力のCSVをフェッチするクラス
 * @author ymdsmn@gmail.com
 *
 */
public class FetchData {

	private static final String data_url = "http://www.tepco.co.jp/forecast/html/images/juyo-j.csv";

    /**
     * 東京電力のCSVをフェッチして軽く整形して返却
     * @return CSVの各行の配列
     * @throws MalformedURLException, IOException, Exception
     */
	public static String[] doFetch ()
				throws MalformedURLException, IOException, Exception {
		URL url = new URL(data_url);
		BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));

		String line;
		// 空行まで読み飛ばす＆ヘッダも飛ばす
		while ((line = reader.readLine()) != null)
			if (0 == line.length())
				break;
		reader.readLine();

		// 一行ずつ読み出す
		String[] lines = new String[24];
		int i = 0;
		try {
			while ((line = reader.readLine()) != null) {
				lines[i++] = line.trim();
			}
		} catch (ArrayIndexOutOfBoundsException e) {
			throw new Exception("Invalid CSV Format");
		} finally {
			if (i != 24)
				throw new Exception("Invalid CSV Format");
		}

		reader.close();

		return lines;
	}

	public static void main (String args[]) throws Exception {
		String[] lines = null;
		lines = FetchData.doFetch();
		for (String line : lines) {
			System.out.println(">> " + line);
		}
	}
}
